package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.fxsh168.server.R;
import com.fxsh168.server.adapters.PassengerInformationAdapter;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.PushOrdersEntity;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class NearOrdersActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    private ListView mMapPassengerInfoListView;
    private List<PushOrdersEntity.DataBean> pushOrdersEntityDataBeanList;
    PassengerInformationAdapter passengerInfoAdapter;
    String mPhone, token, account;
    double mLatitude, mLongitude;
    LinearLayout mOrderParent;
    private int mPosition;
    String id;
    PushOrdersEntity pushOrdersEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_order);
        //用eventbus接收传递过来的值
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");
        Intent intent = getIntent();
        mLatitude = intent.getDoubleExtra("lat", 0.000000);
        mLongitude = intent.getDoubleExtra("log", 0.000000);
        mOrderParent = (LinearLayout) findViewById(R.id.mOrderParent);
        mMapPassengerInfoListView = (ListView) findViewById(R.id.mapPassengerInformation);
        pushOrdersEntityDataBeanList = new ArrayList<>();     //创建数据源
        //创建适配器  先绑定数据源
        getOrdrs();
        mMapPassengerInfoListView.setOnItemClickListener(this);
    }

    //    list的点击事件
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        mPosition = i;
        Intent intent = new Intent(NearOrdersActivity.this, NearOrderDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("position", mPosition);
        bundle.putSerializable("wholeOrderEntity", pushOrdersEntity);
        intent.putExtra("bundle", bundle);
        intent.setAction("wholeOrder");
        startActivity(intent);
    }

    //获取订单
    private void getOrdrs() {
        OkHttpUtils.post().url(Constant.NEARORDERS).addParams("phone", mPhone).addParams("token", token)
                .addParams("lat", mLatitude + "").addParams("lng", mLongitude + "").build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        showToast("连接不上服务器");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("tag", "获取附近的订单" + response);
                        pushOrdersEntity = new Gson().fromJson(response, PushOrdersEntity.class);
                        List<PushOrdersEntity.DataBean> data = pushOrdersEntity.getData();
                        pushOrdersEntityDataBeanList.addAll(data);
                        passengerInfoAdapter = new PassengerInformationAdapter(pushOrdersEntityDataBeanList, NearOrdersActivity.this);
                        mMapPassengerInfoListView.setAdapter(passengerInfoAdapter);
                        passengerInfoAdapter.notifyDataSetChanged();
                    }
                });
    }

}
